#include <rtthread.h>
#include "dio_bsp.h"
#include "local_status.h"
#include "req_execution.h"
#include "sys_conf.h"

void sys_set_remap_status(uint8_t reg_no, uint8_t sbit_pos, uint8_t bit_action)
{
    extern sys_reg_st g_sys;
    if (bit_action == 1) {
        g_sys.status.status_remap[reg_no] |= (0x0001 << sbit_pos);
    } else {
        g_sys.status.status_remap[reg_no] &= ~(0x0001 << sbit_pos);
    }
}

uint16_t sys_get_remap_status(uint8_t reg_no, uint8_t rbit_pos)
{
    extern sys_reg_st g_sys;
    return ((g_sys.status.status_remap[reg_no] >> rbit_pos) & 0x0001);
}

uint8_t sys_get_di_sts(uint8_t din_channel)
{
    uint8_t           byte_offset, bit_offset;
    extern sys_reg_st g_sys;

    byte_offset = din_channel >> 4;
    bit_offset  = din_channel & 0x0f;
    if ((g_sys.status.din_bitmap[byte_offset] >> bit_offset) & 0X0001) {
        return 1;
    } else {
        return 0;
    }
}

//数字输入状态
void sys_option_di_sts(uint8_t din_channel, uint8_t option)
{
    uint8_t           byte_offset, bit_offset;
    extern sys_reg_st g_sys;

    byte_offset = din_channel >> 4;
    bit_offset  = din_channel & 0x0f;

    if (option) {
        g_sys.status.din_bitmap[byte_offset] |= (0x0001 << bit_offset);
    } else {
        g_sys.status.din_bitmap[byte_offset] &= (~(0x0001 << bit_offset));
    }
}
uint8_t sys_get_do_sts(uint8_t dout_channel)
{
    extern sys_reg_st g_sys;

    if (g_sys.status.dout_bitmap & (0x0001 << dout_channel)) {
        return 1;
    } else {
        return 0;
    }
}

void sys_running_mode_update(void)
{
    extern sys_reg_st g_sys;

    g_sys.status.general.running_mode = (g_sys.config.general.testing_mode_en << 2) | (g_sys.config.general.diagnose_mode_en << 1) | (g_sys.config.general.alarm_bypass_en << 0);
    {
        if (sys_get_do_sts(DO_FAN_BPOS) == 1) {
            sys_set_remap_status(WORK_MODE_STS_REG_NO, FAN_STS_BPOS, 1);
        } else {
            sys_set_remap_status(WORK_MODE_STS_REG_NO, FAN_STS_BPOS, 0);
        }
    }

    return;
}

uint16_t sys_get_pwr_sts(void)
{
    extern sys_reg_st g_sys;
    if ((g_sys.status.status_remap[WORK_MODE_STS_REG_NO] >> PWR_STS_BPOS) & 0X0001) {
        return 1;
    } else {
        return 0;
    }
}
uint8_t sys_get_mbm_online(uint8_t mbm_dev, uint8_t sort)
{
    extern sys_reg_st g_sys;
    uint8_t           rec;
    rec = ((g_sys.status.status_remap[sort]) & (0X01 << mbm_dev)) ? 1 : 0;
    return rec;
}
